<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .content {
            width: 1000px;
            margin: 30px auto;
        }

        table {
            width: 100%;
        }

        table,
        td,
        th {
            border: 1px solid #000;
            border-collapse: collapse;
            /* 合并为一行 */
            line-height: 35px;
        }

        tr:nth-of-type(even) {
            background-color: #efefef;
        }

        input {
            margin: 30px 0;
            outline: none;
            border: 1px solid rgb(221, 72, 72);
            padding-left: 15px;
            border-radius: 5px;
            height: 40px;
        }

        p {
            text-align: center;
            display: none;
        }
    </style>
</head>

<body>
    <div class="content">
        <div>
                <input type="text" placeholder="搜索" />
            
        </div>
<table>
        <thead>
            <tr>
                <th>序号</th>
                <th>姓名</th>
                <th>性别</th>
                <th>年龄</th>
                <th>手机号</th>
                <th>描述</th>
                <th>分组</th>
            </tr>
        </thead> 
        <tbody>

        </tbody>
       </table>
        <p>空</p>
    </div>
    <script src="./学生数据.js"></script>
    <script>
        // console.log(data);

        function list(data) {
            if (data.length < 1) {
                document.querySelector('p').style.display = 'block';
                document.querySelector('tbody').innerHTML = '';
                return;
            }
            document.querySelector('p').style.display = 'none';
            var str = '';
            for (var i = 0; i < data.length; i++) {
                //  console.log(data[i]);
                str += `<tr>
                        <td>${i + 1}</td>
                        <td>${data[i].name}</td>
                        <td>${data[i].sex}</td>
                        <td>${data[i].age}</td>
                        <td>${data[i].phone}</td>
                        <td>${data[i].dis}</td>
                        <td>${data[i].fenzu}</td>
                    </tr >`;
                    console.log(str);
            }
            console.log(document.querySelector('tbody'));
            document.querySelector('tbody').innerHTML = str;
        }
        list(data)

        var inp = document.querySelector('input');
        inp.onchange = function () {
            // console.log(this.value);
            var reg = new RegExp(this.value, 'g');

            var arr = [];
            for (var k = 0; k < data.length; k++) {
                var res = false;
                for (var key in data[k]) {
                    if (reg.test(data[k][key])) {
                        res = true;
                    }
                }
                if (res) arr.push(data[k]);
            }
            list(arr);
        }
    </script>
</body>

</html>